12-10-2020

Gramatyka grafiki: ustrukturyzowany opis wizualizacji danych.

Gramatyka grafiki

Układ współrzędnych

Układ współrzędnych

Układ współrzędnych (coord): ramy, w których osadzona jest historia.

Układ współrzędnych

df = data.frame(x = c(0,1,2), y = c(5,6,7))
p1 <- ggplot(df, aes(x=x, y=y)) +
  xlab("") +
  ylab("")
p2 <- ggplot(df, aes(x=x, y=y)) +
  xlab("") +
  ylab("") +
  coord_flip()
p1 + p2

Warstwy

Dane

Dane

Dane (data): tabela, w której obserwacje opisane są przez wektor chech.

##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa

Mapowania

Mapowania

Mapowania (aes): pary cech obserwacji i atrybutów wykresu.

Mapowania

p1 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(size = 4)

p2 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(size = 4)

p3 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Sepal.Length)) +
  geom_point(size = 4)

p4 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, shape = Species)) +
  geom_point(size = 4)

(p1 + p2) / (p3 + p4)

Formy

Formy

Formy (geom): graficzna reprezentacja wartości liczbowych.

Formy

Formy (geom): graficzna reprezentacja wartości liczbowych.

p1 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(size = 4)

p2 <- ggplot(iris, aes(x = Species, y = Sepal.Length)) +
  stat_summary(fun.y = "mean", geom = "bar")

p3 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_line()

p4 <- ggplot(iris, aes(x = Species, y = Sepal.Length, color = Species)) +
  geom_boxplot()


(p1 + p2) / (p3 + p4)

Statystyki

Statystyki

Statystyki (stat): agregaty danych

Statystyki

Statystyki (stat): agregaty danych

p1 <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
  geom_density(alpha = 0.5)
p2 <- ggplot(iris, aes(x = Species, y = Sepal.Length, color = Species)) +
  geom_boxplot()
p3 <- ggplot(iris, aes(x = Species, y = Sepal.Length, color = Species)) +
  geom_violin()
p4 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, fill = Species)) +
  stat_density2d(aes(alpha = ..level..), geom = "polygon", color = "black")


(p1 + p2) / (p3 + p4)

Przesunięcia

Przesunięcia

Przesunięcia (position): modyfikatory położenia.

Przesunięcia

Przesunięcia (position): modyfikatory położenia.

p1 <- ggplot(iris, aes(x = Species, y = Sepal.Length, color = Species)) +
  geom_jitter(position = "identity")+
  theme(legend.position = "bottom")
p2 <- ggplot(iris, aes(x = Species, y = Sepal.Length, color = Species)) +
  geom_jitter(position = "jitter") +
  theme(legend.position = "bottom")

(p1 + p2)

Panele

Panele

Panele (facets): fragmenty historii o teh samej strukturze dla podzbiorów danych.

Skale

Skale

Skale (scale): transformacja cech obserwacji na atrybuty wykresu.

Skale

p1 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() 

p2 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  scale_x_reverse()

p1 + p2

Dekoracje

Dekoracje

Dekoracje (theme): styl wizualny.

Dekoracje

Dekoracje (theme): styl wizualny.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species)) +
  geom_point(size = 4) +
  theme_economist() +
  ggtitle("Theme: economist")

Dekoracje

Dekoracje (theme): styl wizualny.

Dekoracje

Dekoracje (theme): styl wizualny.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species)) +
  geom_point(size = 4) +
  theme_tufte() +
  ggtitle("Theme: Tufte")

Dekoracje

Dekoracje (theme): styl wizualny.

Dekoracje

Dekoracje (theme): styl wizualny.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species)) +
  geom_point(size = 4) +
  theme_excel() +
  ggtitle("Theme: Excel")

Do poczytania